package com.smmisia.article.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smmisia.article.api.AuthService;
import com.smmisia.article.config.WebSocketServer;
import com.smmisia.article.dto.user.UserDTO;
import com.smmisia.article.entity.Friend;
import com.smmisia.article.entity.User;
import com.smmisia.article.enums.FriendEnum;
import com.smmisia.article.exception.SocketException;
import com.smmisia.article.factory.FriendStatusFactory;
import com.smmisia.article.factory.FriendStrategy;
import com.smmisia.article.mapper.FriendMapper;
import com.smmisia.article.request.FriendAddRequest;
import com.smmisia.article.response.socket.FriendResponse;
import com.smmisia.article.service.ArticleService;
import com.smmisia.article.service.FriendService;
import com.smmisia.common.Result.Result;
import com.smmisia.common.entity.auth.convertor.UserConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend>
    implements FriendService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private AuthService authService;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private FriendStatusFactory friendStatusFactory;

    private final String APPLY_FRIEND = "apply_friend:";

    @Override
    public Result<String> add(FriendAddRequest request) {
        Friend friend = Friend.create(request);
        boolean save = this.save(friend);
        Assert.isTrue(save,()->new SocketException("添加好友失败"));
        redisTemplate.opsForHash().put(APPLY_FRIEND + StpUtil.getLoginIdAsInt(),request.getUserId(),request.getContent());
        return Result.ok("申请成功");
    }


    @Override
    public Result<List<String>> online() {
        List<String> onlineUser = webSocketServer.getOnlineUser();
        return Result.ok(onlineUser);
    }

    @Override
    public Result<String> accept(Integer id,Integer status) {
        FriendStrategy friendStrategy = friendStatusFactory.getFriendStrategy(status);
        friendStrategy.updateFriendStatus(id);
        return Result.ok("操作成功");
    }
}




